/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017-2025 Ta4j Organization & respective
 * authors (see AUTHORS)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package ta4jexamples.charting;

import org.ta4j.core.AnalysisCriterion;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseTradingRecord;
import org.ta4j.core.Trade;
import org.ta4j.core.TradingRecord;
import org.ta4j.core.indicators.CachedIndicator;
import org.ta4j.core.num.Num;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * An indicator that visualizes an {@link AnalysisCriterion} over time by
 * calculating the criterion value at each bar index using a partial trading
 * record.
 *
 * <p>
 * For each bar index, this indicator creates a partial trading record
 * containing only positions that have been entered (and optionally closed) up
 * to that index, then calculates the criterion value for that partial record.
 * This allows analysis criteria to be visualized as time series on charts.
 * </p>
 *
 * @since 0.19
 */
public class AnalysisCriterionIndicator extends CachedIndicator<Num> {

    private final AnalysisCriterion criterion;
    private final TradingRecord fullTradingRecord;
    private final List<Trade> allTrades;
    private final String label;

    /**
     * Constructs an AnalysisCriterionIndicator.
     *
     * @param series        the bar series
     * @param criterion     the analysis criterion to visualize
     * @param tradingRecord the full trading record
     * @throws NullPointerException if any parameter is null
     */
    public AnalysisCriterionIndicator(BarSeries series, AnalysisCriterion criterion, TradingRecord tradingRecord) {
        super(series);
        this.criterion = Objects.requireNonNull(criterion, "Criterion cannot be null");
        this.fullTradingRecord = Objects.requireNonNull(tradingRecord, "Trading record cannot be null");
        this.allTrades = new ArrayList<>(tradingRecord.getTrades());
        this.label = deriveLabel(criterion);
    }

    @Override
    protected Num calculate(int index) {
        // Create a partial trading record with trades up to this index
        TradingRecord partialRecord = createPartialTradingRecord(index);

        // Calculate criterion value for the partial record
        return criterion.calculate(getBarSeries(), partialRecord);
    }

    @Override
    public int getCountOfUnstableBars() {
        // Analysis criteria don't have unstable bars - they calculate from trading
        // records
        return 0;
    }

    /**
     * Creates a partial trading record containing only trades that have occurred up
     * to the specified index.
     *
     * @param upToIndex the maximum bar index to include
     * @return a partial trading record
     */
    private TradingRecord createPartialTradingRecord(int upToIndex) {
        // Filter trades where trade index <= upToIndex
        List<Trade> partialTrades = new ArrayList<>();
        for (Trade trade : allTrades) {
            if (trade.getIndex() <= upToIndex) {
                partialTrades.add(trade);
            }
        }

        // Create new trading record from filtered trades
        if (partialTrades.isEmpty()) {
            // Return empty record with same cost models and starting type
            return new BaseTradingRecord(fullTradingRecord.getStartingType(),
                    fullTradingRecord.getTransactionCostModel(), fullTradingRecord.getHoldingCostModel());
        }

        Trade[] tradesArray = partialTrades.toArray(new Trade[0]);
        return new BaseTradingRecord(fullTradingRecord.getTransactionCostModel(),
                fullTradingRecord.getHoldingCostModel(), tradesArray);
    }

    @Override
    public String toString() {
        return label;
    }

    private static String deriveLabel(AnalysisCriterion criterion) {
        String simpleName = criterion.getClass().getSimpleName();
        if (simpleName.endsWith("Criterion") && simpleName.length() > "Criterion".length()) {
            simpleName = simpleName.substring(0, simpleName.length() - "Criterion".length());
        }
        return simpleName.isEmpty() ? criterion.getClass().getSimpleName() : simpleName;
    }
}
